/**
 * DAT文本清理工具
 * 专门用于处理DAT文件中重复词和格式问题
 */

/**
 * 清理DAT格式的英语文本，处理重复词和特殊格式
 * @param {string} englishPart - 原始英语部分
 * @returns {string} 清理后的英语文本
 */
function cleanDatEnglishText(englishPart) {
  // 首先处理DAT特有的格式: word1$word2$$$$$
  // 按照需求，应该把一个$和四个$之间的内容全部去掉
  // 即保留第一个词，去掉$和后面的内容

  // 处理标准格式: "word$word$$$$$" -> "word"
  let cleanedText = englishPart.replace(/(\b\w+)\$\w+\${4,}/g, "$1");

  // 处理多词格式: "due to$due to$$$$$" -> "due to"
  cleanedText = cleanedText.replace(/(\b[\w\s]+)\$[\w\s]+\${4,}/g, "$1");

  // 处理以$开头的格式: "$you words$$$$$" -> "" (去掉$和后面的全部内容)
  cleanedText = cleanedText.replace(/\$[^$]*\${4,}/g, "");

  // 处理特殊标记
  const words = cleanedText
    .replace(/\^/g, " ")
    .replace(/\$/g, " ")
    .replace(/◆/g, "")
    .split(" ")
    .filter(word => word.trim() !== "");

  // 去除重复单词（包括词组）
  const cleanedWords = [];
  for (let i = 0; i < words.length; i++) {
    // 检查当前词是否与下一个词相同（简单的重复词处理）
    if (i < words.length - 1 && words[i].toLowerCase() === words[i + 1].toLowerCase()) {
      cleanedWords.push(words[i]);
      i++; // 跳过下一个相同的单词
    } else {
      cleanedWords.push(words[i]);
    }
  }

  // 进一步处理词组级别的重复
  let result = cleanedWords.join(" ");

  // 使用正则表达式处理重复的词组
  // 匹配模式: 词组 + 空格 + 相同词组
  // 例如: "due to due to" 或 "in view of in view of"
  result = result.replace(/(\b\w+(?:\s+\w+)*?)\s+\1\b/gi, "$1");

  // 处理缩写词
  const contractions = {
    "can not": "can't",
    "don not": "don't",
    "do not": "don't",
    "will not": "won't",
    "have not": "haven't",
    "had not": "hadn't",
    "would not": "wouldn't",
    "could not": "couldn't",
    "should not": "shouldn't",
    "were not": "weren't",
    "is not": "isn't",
    "are not": "aren't",
    "does not": "doesn't",
    "did not": "didn't",
  };

  for (const [original, contracted] of Object.entries(contractions)) {
    const regex = new RegExp(`\\b${original}\\b`, "gi");
    result = result.replace(regex, contracted);
  }

  return result.trim();
}

module.exports = {
  cleanDatEnglishText,
};
